package com.itheima.service.impl;
/*
    ヾ(❀ﾟ▽ﾟ)ノ 
 
 Grey1996year@163.com 
 
*/

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.MenuMapper;
import com.itheima.mapper.RoleMapper;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

	@Autowired
	private MenuMapper menuMapper;
	@Autowired
	private RoleMapper roleMapper;


	@Override
	public PageResult findPage(QueryPageBean queryPageBean) {
		Integer currentPage = queryPageBean.getCurrentPage();
		Integer pageSize = queryPageBean.getPageSize();
		String queryString = queryPageBean.getQueryString();
		//
		PageHelper.startPage(currentPage,pageSize);
		//
		Page<Menu> page = menuMapper.queryByCondition(queryString);


		return new PageResult(page.getTotal(),page.getResult());
	}

	@Override
	public void add(Menu menu) {
		// 1.先添加menu表
		menuMapper.add(menu);

	}

	@Override
	public Menu findById(Integer id) {

		Menu menu = menuMapper.findById(id);


		return menu;
	}

	@Override
	public void update(Menu menu) {
		menuMapper.update(menu);


	}

	@Override
	public void delete(Integer id) {
		menuMapper.delete(id);
	}

	@Override
	public List<Map<String,Object>> findMenu2Show(String username) {

		List<Map<String,Object>> parentList = new ArrayList<>();

		List<Menu> menuList = menuMapper.findAllMenu(username);
		//遍历集合,拿到每一个menu
		for (Menu menu : menuList) {
			//map1封装一级菜单
			Map<String,Object> map1 = new HashMap<>();
			map1.put("path",menu.getPath());
			map1.put("title",menu.getName());
			map1.put("icon",menu.getIcon());

			List<Map<String,Object>> childrenList = new ArrayList<>();
			//拿到menuId,去查
			Integer menuId = menu.getId();
			List<Menu> childrenMenuList = menuMapper.findChildren(menuId);
			//遍历
			for (Menu menu1 : childrenMenuList) {
				//map2封装二级菜单
				Map<String,Object> map2 = new HashMap<>();
				map2.put("path",menu1.getPath());
				map2.put("title",menu1.getName());
				map2.put("linkUrl",menu1.getLinkUrl());
				childrenList.add(map2);
			}
			map1.put("children",childrenList);

			parentList.add(map1);
		}


		return parentList;
	}

}
